package com.hanhai.material.device.controller;


import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.device.entity.SysBadRecord1;
import com.hanhai.material.device.entity.SysProductRecord;
import com.hanhai.material.device.entity.SysReasonType1;
import com.hanhai.material.device.service.SysBadRecord1Service;
import com.hanhai.material.device.service.SysReasonType1Service;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

import static com.hanhai.material.device.entity.table.SysBadRecord1TableDef.SYS_BAD_RECORD1;
import static com.hanhai.material.device.entity.table.SysReasonType1TableDef.SYS_REASON_TYPE1;
import static com.mybatisflex.core.query.QueryMethods.sum;

/**
 *  控制层。
 *
 * @author youchuang
 * @since 2024/04/28 17:36:30
 */
@RestController
@Api("接口")
@RequestMapping("/reason_type1")
@Log4j2
public class SysReasonType1Controller {

    @Autowired
    private SysReasonType1Service sysReasonType1Service;

    @Autowired
    private SysBadRecord1Service sysBadRecord1Service;

    /**
     * 添加。
     *
     * @param sysReasonType1
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ApiOperation("保存")
    public boolean save(@RequestBody @ApiParam("") SysReasonType1 sysReasonType1) {
        return sysReasonType1Service.save(sysReasonType1);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @ApiOperation("根据主键")
    public boolean remove(@PathVariable @ApiParam("主键") Serializable id) {
        return sysReasonType1Service.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param sysReasonType1
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @ApiOperation("根据主键更新")
    public boolean update(@RequestBody @ApiParam("主键") SysReasonType1 sysReasonType1) {
        return sysReasonType1Service.updateById(sysReasonType1);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list/{workShop}")
    @ApiOperation("查询所有")
    public Result<?> list(@PathVariable String workShop) {
        QueryWrapper queryWrapper= QueryWrapper.create();
        queryWrapper.where(SYS_REASON_TYPE1.PID.eq(0));
        queryWrapper.where(SYS_REASON_TYPE1.WORK_SHOP.eq(workShop));
        List<SysReasonType1> reasonType1List=this.sysReasonType1Service.getMapper().selectListWithRelationsByQuery(queryWrapper);
//        return sysReasonType1Service.list();
        return reasonType1List.isEmpty()? ResultTool.fail():ResultTool.success(reasonType1List);
    }

    @PostMapping("/listByCondition")
    public Result<?> getByCondition(@RequestBody SysProductRecord sysProductRecord){
        QueryWrapper queryWrapper= QueryWrapper.create();
        queryWrapper.where(SYS_REASON_TYPE1.PID.eq(0));
        queryWrapper.where(SYS_REASON_TYPE1.WORK_SHOP.eq(sysProductRecord.getWorkShop()));
        List<SysReasonType1> sysReasonTypeList=this.sysReasonType1Service.getMapper().selectListWithRelationsByQuery(queryWrapper);
        for(SysReasonType1 reasonType1:sysReasonTypeList){
            int prenium=0;
            for(SysReasonType1 reasonType1Child:reasonType1.getReasonType1List()){
                for(SysReasonType1 reason:reasonType1Child.getReasonType1List()){
                    queryWrapper=QueryWrapper.create();
                    queryWrapper.where(SYS_BAD_RECORD1.PRODUCT_RECORD_ID.eq(sysProductRecord.getId()));
                    queryWrapper.where(SYS_BAD_RECORD1.REASON_TYPE_ID.eq(reasonType1.getId()));
                    queryWrapper.where(SYS_BAD_RECORD1.REASON_TYPE_ID1.eq(reasonType1Child.getId()));
                    queryWrapper.where(SYS_BAD_RECORD1.REASON_ID.eq(reason.getId()));
                    queryWrapper.select(sum(SYS_BAD_RECORD1.NUM).as("num"));
                    log.error("SQL:{}",queryWrapper.toSQL());
                    SysBadRecord1 badRecord1=this.sysBadRecord1Service.getOne(queryWrapper);
                    if(badRecord1!=null){
                        reason.setNum(badRecord1.getNum());
                    }

                }
                int sum=reasonType1Child.getReasonType1List().stream().mapToInt(SysReasonType1::getNum).sum();
                if(reasonType1Child.getReasonTypeName().equals("极品")){
                    prenium=sum;
                }
                reasonType1Child.setNum(sum);

            }
            int sum=reasonType1.getReasonType1List().stream().mapToInt(SysReasonType1::getNum).sum();
            reasonType1.setNum(sum-prenium);
        }
        return sysReasonTypeList.isEmpty()?ResultTool.fail():ResultTool.success(sysReasonTypeList);
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    @ApiOperation("根据主键获取")
    public SysReasonType1 getInfo(@PathVariable @ApiParam("主键") Serializable id) {
        return sysReasonType1Service.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    @ApiOperation("分页查询")
    public Page<SysReasonType1> page(@ApiParam("分页信息") Page<SysReasonType1> page) {
        return sysReasonType1Service.page(page);
    }

}
